import {defineStore} from 'pinia'
import {reqCode,reqUserLogin} from '@/api/hospital/index'
import {UserInfoResponseData,LoginData,UserInfo} from '@/api/hospital/type'
import {UserState} from '@/store/modules/type'
let useUserStore = defineStore('user',{
    state:():UserState=>{
        return{
            visiable:false,
            code:'',
            userInfo:JSON.parse((localStorage.getItem('userInfo') as string)) || {}
        }
    },
    actions:{
        // 获取验证码
       async getCode(phone:string){
            let res = await reqCode(phone)
            if(res.code==200){
                this.code = res.data
                return 'ok'
            }else{
                return Promise.reject(new Error(res.message))
            }
        },
        // 登录
      async userLogin(loginParam:LoginData){
            let res:UserInfoResponseData = await reqUserLogin(loginParam)
            if(res.code==200){
                this.userInfo = res.data
                localStorage.setItem('userInfo',JSON.stringify(this.userInfo))
                return 'ok'
            }else{
                return Promise.reject(new Error(res.message))
            }
        },
        // 退出登录
        loginOut(){
            this.userInfo={token:'',name:''}
            localStorage.removeItem('userInfo')
        }
    },
    getters:{

    }
})
export default useUserStore;